package com.more.offer.algorithm.leetcode.lcode03;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: yinpeng
 * @Date: 2019/9/25 12:00
 * @Description: TODO
 **/
public class Solution_02 {
	public static int lengthOfLongestSubstring(String s) {
		if(s.isEmpty()) return 0;
		Map<Character,Integer> map = new HashMap<>();
		int startIndex = 0;
		int maxLength = 0;
		for (int i = 0; i < s.length(); i++) {
			if (map.containsKey(s.charAt(i))) {
				//将窗口左端向右滑动，滑动到重复元素 索引+1 的位置
				startIndex = Math.max(startIndex, map.get(s.charAt(i)) + 1);
			}
			map.put(s.charAt(i),i);
			//i-startIndex+1 计算当前窗口长度
			maxLength = Math.max(maxLength,i-startIndex+1);
		}
		return maxLength;
	}

	public static void main(String[] args) {
		String s = "abcdbcdadacefghj";
		int i = lengthOfLongestSubstring(s);
		System.out.println(i);
	}
}
